package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.SearchCityActivity;
import a66weding.com.jiehuntong.activity.ToolsGiftAddGiftActivity;
import a66weding.com.jiehuntong.activity.ToolsGiftsActivity;
import a66weding.com.jiehuntong.activity.ToolsWedBudgetActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GiftBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.common.MyViewHolder;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/2
 * @description :
 */
public class ToolsPager_Budget_Home extends BasePager implements View.OnClickListener, OnNetResultListener {
    private ToolsWedBudgetActivity mBudgetActivity;
    private View mContentview;
    private LinearLayout mEmptyLay;
    private TextView mTitleTv;
    private TextView mBackTv;
    private TextView mAddBudgetTv;
    private RelativeLayout mContentRlay;
    private Button mBudgetCommit;
    private EditText mMoneyCountEt;
    private EditText mSeatCountEt;

    private LinearLayout mBackLay;
    private ProgressBar mProgressbar;
    private LinearLayout mSelectPlaceLay;
    private TextView mPlaceTv;

    private String mobileNum;




    public ToolsPager_Budget_Home(Activity activity) {

        super(activity);

    }


    @Override
    protected View getView() {
        mBudgetActivity= (ToolsWedBudgetActivity) mActivity;

        mContentview = View.inflate(mActivity, R.layout.pager_tools_budget_home,null);
        mTitleTv = (TextView) mContentview.findViewById(R.id.tv_tools_dayplan_title);
        mBackTv = (TextView) mContentview.findViewById(R.id.tv_tools_dayplan_backcontent);
        mBackLay = (LinearLayout) mContentview.findViewById(R.id.llay_tools_wedregister_back);
        mEmptyLay = (LinearLayout) mContentview.findViewById(R.id.llay_tools_gift_emptygift);
        mAddBudgetTv = (TextView) mContentview.findViewById(R.id.tv_tools_gift_addgift);
        mContentRlay = (RelativeLayout) mContentview.findViewById(R.id.rlay_tools_budgetcontent);
        mBudgetCommit = (Button) mContentview.findViewById(R.id.btn_tools_budget_editcommit);
        mMoneyCountEt = (EditText) mContentview.findViewById(R.id.et_tools_budget_moneycount);
        mSeatCountEt = (EditText) mContentview.findViewById(R.id.et_tools_budget_seatcount);
        mSelectPlaceLay = (LinearLayout) mContentview.findViewById(R.id.lay_tools_wedregister_place);
        mPlaceTv = (TextView) mContentview.findViewById(R.id.tv_tools_wedregistry_place);


        mTitleTv.setText("结婚预算");
        mBackLay.setOnClickListener(this);
        mAddBudgetTv.setOnClickListener(this);
        mBudgetCommit.setOnClickListener(this);
        mSelectPlaceLay.setOnClickListener(this);

        mPlaceTv.setText( mBudgetActivity.getCityName());

        mEmptyLay.setVisibility(View.VISIBLE);

        return mContentview;
    }


    @Override
    public void initData() {



    }

    @Override
    public void notifyData() {
        mPlaceTv.setText( mBudgetActivity.getCityName());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

                case R.id.tv_tools_gift_addgift://添加预算
                    mEmptyLay.setVisibility(View.GONE);
                    mContentRlay.setVisibility(View.VISIBLE);
                    break;
                case R.id.btn_tools_budget_editcommit://提交
                        boolean isRight=checkInput();
                        if(isRight) {
                            String moneyCount = mMoneyCountEt.getText().toString().trim();
                            int mCount = Integer.valueOf(moneyCount);
                            mBudgetActivity.setBudgetCount(mCount);
                            String seatCount = mSeatCountEt.getText().toString().trim();
                            int mSeatCount = Integer.valueOf(seatCount);
                            mBudgetActivity.setTableCount(mSeatCount);

                            String url=UrlDealUtil.getRealUrl(RequestCode.RETURN_WBITEMSYS);

                            HashMap<String,String> requestParams=new HashMap<String,String>();
                            requestParams.put("BudgetMoney",""+mCount);
                            requestParams.put("BudgetTables",""+mSeatCount);

                            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                                    this, BudgetBean.class, RequestCode.RETURN_WBITEMSYS, requestParams);

                            HttpHandleUtil.executeCommonPost(mBudgetActivity,httpRequestBean);
                            showCommitPopupWindow(view);

                        }


                    break;
                case R.id.llay_toolsheader_back://返回
                    mBudgetActivity.getSupportFragmentManager().popBackStack();
                    mBudgetActivity.finish();
                    break;
                case R.id.llay_tools_wedregister_back://返回
                    mBudgetActivity.getSupportFragmentManager().popBackStack();
                    mBudgetActivity.finish();
                    break;
                case R.id.lay_tools_wedregister_place://城市选择
                        Intent intent = new Intent(mBudgetActivity, SearchCityActivity.class);
                        intent.putExtra("city",mPlaceTv.getText().toString().trim());
                        mBudgetActivity.startActivityForResult(intent,mBudgetActivity.getmPlaceResultCode());

                    break;

        }
    }

    private boolean checkInput() {
        String moneyCount = mMoneyCountEt.getText().toString().trim();
        if(TextUtils.isEmpty(moneyCount)) {
            CUtils.showToast("输入的金额不能为空");
            return false;
        }
        int mCount = Integer.valueOf(moneyCount);
        if(mCount<20000) {
            CUtils.showToast("输入的金额不能小于2万");
            return false;
        }
        String seatCount = mSeatCountEt.getText().toString().trim();
        if(TextUtils.isEmpty(seatCount)) {
            CUtils.showToast("输入的婚宴桌数不能为空");
            return false;
        }
        int mSeatCount = Integer.valueOf(seatCount);
        if(mCount<1) {
            CUtils.showToast("输入的婚宴桌数不能小于1桌");
            return false;
        }

        return true;
    }


    @Override
    public void onSucess(SuccessBean sucessbean)  {
        if(mCommitpop!=null) {
            mCommitpop.dismiss();
        }
        mEmptyLay.setVisibility(View.GONE);

        Object data = sucessbean.getData();

        if(data instanceof BudgetBean) {
            BudgetBean mBudgitBean= (BudgetBean) data;
            mBudgetActivity.setmBudgitData(mBudgitBean);
            mBudgetActivity.setFragment("结婚预算明细页");

            switch (mBudgitBean.getResCode()) {
                case "0000":

                    mProgressbar.setVisibility(View.GONE);
                    mEmptyLay.setVisibility(View.GONE);
                    List<BudgetBean.DataBean> budgitList = mBudgitBean.getData();
                    mBudgetActivity.setmBudgitData(mBudgitBean);
                    break;
                case "0001":

                case "0002":

                case "0003":
                    mEmptyLay.setVisibility(View.VISIBLE);
                    break;
            }


        }else if(data instanceof CommonResultBean){
            CommonResultBean commonResult= (CommonResultBean) data;
            switch (commonResult.getResCode()) {
                    case "0000":
                    break;
            }
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
        if(mCommitpop!=null) {
            mCommitpop.dismiss();
        }
        CUtils.showToast("数据请求错误，请重试");
    }





}
